﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace AbhiRuchiFoods
{
    /// <summary>
    /// Interaction logic for SearchEmployeeInformation.xaml
    /// </summary>
    public partial class SearchEmployeeInformation : UserControl
    {
        public SearchEmployeeInformation()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            int empType;
            string empName,empNo;
            empType=Convert.ToInt32(((ComboBoxItem)cmbEmpType.SelectedItem).ToolTip.ToString());
            empName=txtEmpName.Text;
            empNo=txtEmpId.Text;
            DataTable dtEmp=new DataTable();
            dtEmp=AbhiruchiDAL.SearchEmployee(empType, empName, empNo);
            if (dtEmp.Rows.Count == 0)
                MessageBox.Show("Sorry, Employee Not Found", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            gvEmp.ItemsSource = dtEmp;
        }

        private void gvEmp_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            btnEdit.IsEnabled = true;
            lblEmpNo.Content = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();

            DataTable dtEmpPhoto = new DataTable();
            dtEmpPhoto = AbhiruchiDAL.GetEmployeePhoto(lblEmpNo.Content.ToString());
            if (dtEmpPhoto.Rows[0][0].ToString()!= "")
            {
                ImageSourceConverter imgs = new ImageSourceConverter();
                imgEmp.SetValue(Image.SourceProperty, imgs.ConvertFromString(dtEmpPhoto.Rows[0][0].ToString()));
            }

            lblEmpFullName.Content = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();
            lblPhoneNo.Content = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();
            if (lblEmpNo.Content.ToString().Contains("PE"))
            {
                lblEmailId.Content = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[3])).Value.ToString();
            }
            else
            {
                lblEmailId.Content = "";
            }
            lblPermanentAddress.Content = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[4])).Value.ToString();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (lblEmpNo.Content.ToString().Contains("PE"))
            {
                EmployeeInformation.NavigateEmpNo = lblEmpNo.Content.ToString();
                //MainWindow obj = new MainWindow();
                //obj.openNewPermanentEmployeeTab();
                EmployeeInformation p = new EmployeeInformation();
                this.Content = p;
            }
            else
            {
                TemporaryEmployeeInformation.NavigateEmpNo = lblEmpNo.Content.ToString();
                TemporaryEmployeeInformation p = new TemporaryEmployeeInformation();
                this.Content = p;  
                
            }
        }

        private void ClearAllFields()
        {
            txtEmpName.Text = "";
            txtEmpId.Text = "";
            lblEmpNo.Content = "";
            lblEmpFullName.Content = "";
            lblPhoneNo.Content = "";
            lblEmailId.Content = "";
            lblPermanentAddress.Content = "";
            btnEdit.IsEnabled = false;
            gvEmp.ItemsSource = null;
            cmbEmpType.SelectedIndex = 0;
        }

        private void btnClearSearch_Click(object sender, RoutedEventArgs e)
        {
            ClearAllFields();   
        }
        
    }
}
